package com.liu.lsm.livestock.controller;

import com.liu.lsm.livestock.entity.History;
import com.liu.lsm.livestock.service.HistoryService;
import com.liu.lsm.utils.ResultObj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/history")
public class HistoryController {

    @Autowired
    HistoryService historyService;

    /**
     * 查询单个牛羊历史信息
     */
    @RequestMapping("/queryOne")
    public List<History> selectById(Integer livestockId) {
        return historyService.selectById(livestockId);
    }

    /**
     * 条件查询
     */
    @RequestMapping("/select")
    public List<History> listAll(History history) {
        return historyService.listAll(history);
    }


    /**
     * 添加记录
     */
    @RequestMapping("/addHistory")
    public ResultObj addHistory(History history) {
        try {
            this.historyService.insertById(history);
            return ResultObj.ADD_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.ADD_ERROR;
        }
    }


    /**
     * 修改记录
     */
    @RequestMapping("/updateHistory")
    public ResultObj updateHistory(History history) {
        try {
            this.historyService.updateById(history);
            return ResultObj.UPDATE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.UPDATE_ERROR;
        }
    }

    /**
     * 删除记录
     */
    @RequestMapping("/deleteHistory")
    public ResultObj deleteHistory(Integer HistoryId) {
        try {
            this.historyService.deleteById(HistoryId);
            return ResultObj.DELETE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }

    }


}
